//
//  YSCreateRoleViewController.m
//  Yaoyle
//
//  Created by ZhaoXudong on 14-9-23.
//  Copyright (c) 2014年 ZhaoXudong. All rights reserved.
//

#import "YSCreateRoleViewController.h"

#define sex_image_h  [UIImage imageNamed:@"round"]
#define sex_image_n  [UIImage imageNamed:@"round_no"]

@interface YSCreateRoleViewController ()<UITextFieldDelegate> {
    __weak IBOutlet UITextField *roleNameTextField;
    __weak IBOutlet UIButton *sexBoybtn;
    __weak IBOutlet UIButton *sexGirlbtn;
    
    __weak IBOutlet UIImageView *headImage;
    __weak IBOutlet UIScrollView *contentView;
    eSexType sexType;
    
    __weak IBOutlet UIImageView *sexGirlImage;
    __weak IBOutlet UIImageView *sexBoyImage;
    NSDictionary *roleNameInfo;
}

@end

@implementation YSCreateRoleViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        sexType = eSexBoy;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)sexBtnPress:(id)sender {
    UIButton *btn = (UIButton *)sender;
    if (btn == sexBoybtn) {
        sexType = eSexBoy;
        sexBoyImage.image = sex_image_h;
        sexGirlImage.image = sex_image_n;
    } else {
        sexType = eSexGirl;
        sexBoyImage.image = sex_image_n;
        sexGirlImage.image = sex_image_h;
    }
}


- (IBAction)createRole:(id)sender {
    NSString *name = roleNameTextField.text;
    if (name.length == 0 || [YSUtility isBlankObject:name]) {
        [self showMsgWithTitle:@"提示" msg:@"角色昵称不能为空"];
        return;
    }
    [YSProgressHUD showHUDAddedTo:self.view];
    roleNameInfo = @{RoleName:name,
                     RoleSex:[NSString stringWithFormat:@"%d",sexType]};
    
    [[NetworkMgr sharedManager] asynchRequestWithFunId:eAPI_UPDATE_MEMBER
                                              delegate:self
                                                params:roleNameInfo];
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    [contentView setContentOffset:CGPointMake(0, 20) animated:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [contentView setContentOffset:CGPointMake(0, 0) animated:YES];
    [textField resignFirstResponder];
    return YES;
}

#pragma mark - NetworkDelegate
- (void)didSuccess:(NetworkMgr *)api userInfo:(NSDictionary *)info {
    [YSProgressHUD hideHUDForView:self.view];
    NSInteger funId = [info[kFUN_ID] integerValue];
    
    if (funId == eAPI_UPDATE_MEMBER) {
        if (self.block) {
            self.block(roleNameInfo);
            [self.navigationController popViewControllerAnimated:NO];
        }
    }
}
@end
